let id = 0
class OpNode{
  code
  parent: any
  renderProps: any
  dataProps: any
  childrenMap: any
  children: any
  dom: any
  id: any
  forceUpdate: any
  constructor(props:any={}) {
    this.code = props.code
    this.parent = props.parent
    this.renderProps = props.renderProps
    this.dataProps = props.dataProps
    this.childrenMap = props.childrenMap || {}
    this.children = props.children || []
    this.dom = props.dom
    this.id = id++
  }
  setDom(dom: any) {
    this.dom = dom
  }
  setChildren(child: any) {
    this.children = [...this.children, child]
  }
  setParent(parent: any) {
    this.parent = parent
  }
  buildChildren(data:any) {
    data.forEach((item)=>{
      let {childMap, children ,...props} = item
      let node = new OpNode({parent: this, ...props})
      this.setChildren(node)
      if(item.childMap) {
        let keys = Object.keys(item.childMap)
        keys.forEach(key => {
          let obj = item.childMap[key]
          node.buildMapChildren(key, obj)
        })
      }
      if(item.children) {
        node.buildChildren(item.children)
      }
    })
  }
  buildMapChildren(key, item) {
    let {childMap, children ,...props} = item
     let node = new OpNode({parent: this, ...props})
     this.childrenMap[key] =node
    if(item.childMap) {
      let keys = Object.keys(item.childMap)
      keys.forEach(key => {
        let obj = item.childMap[key]
        node.buildMapChildren(key, obj)
      })
    }
    if(item.children) {
      node.buildChildren(item.children)
    }
  }
}

export default OpNode